package adapter;

import java.util.List;

import utils.DataUtils;

import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.mycalendar.MainActivity;
import com.mycalendar.R;

import data.DateInfo;

/**
 * 日历gridview适配器
 * */
public class CalendarAdapter extends BaseAdapter {

	private List<DateInfo> list = null;
	private Context context = null;
	private int selectedPosition = -1;
	MainActivity activity;

	public CalendarAdapter(MainActivity activity, List<DateInfo> list) {
		this.context = activity;
		this.list = list;
		this.activity = activity;
	}

	public List<DateInfo> getList() {
		return list;
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return list.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	/**
	 * 设置选中位置
	 * */
	public void setSelectedPosition(int position) {
		selectedPosition = position;
	}

	/**
	 * 产生一个view
	 * */
	public View getView(int position, View convertView, ViewGroup group) {
		// 通过viewholder做一些优化
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.gridview_item, null);
			viewHolder.date = (TextView) convertView
					.findViewById(R.id.item_date);
			viewHolder.remark = (TextView) convertView
					.findViewById(R.id.item_remark);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		// 根据数据源设置单元格的字体颜色、背景等
		DateInfo info = list.get(position);
		Resources res = context.getResources();

		switch (info.getStatus()) {
		case 0:
			setColors(viewHolder, convertView, info, res,
					R.string.remark_bottom_01, R.color.black, R.color.black,
					R.color.details_color_1);
			break;
		case 1:
			setColors(viewHolder, convertView, info, res,
					R.string.remark_bottom_02, R.color.black, R.color.black,
					R.color.details_color_2);
			break;
		case 2:
			setColors(viewHolder, convertView, info, res,
					R.string.remark_bottom_03, R.color.black, R.color.black,
					R.color.details_color_3);
			break;
		case 3:
			setColors(viewHolder, convertView, info, res,
					R.string.remark_bottom_04, R.color.black, R.color.black,
					R.color.details_color_4);
			break;
		case 4:
			setColors(viewHolder, convertView, info, res,
					R.string.remark_bottom_05, R.color.black, R.color.black,
					R.color.white);
			break;
		case 5:
			setColors(viewHolder, convertView, info, res,
					R.string.remark_bottom_06, R.color.black, R.color.black,
					R.color.details_color_5);
			break;
		case 6:
			setColors(viewHolder, convertView, info, res,
					R.string.remark_bottom_07, R.color.black, R.color.black,
					R.color.details_color_6);
			break;
		case 7:
			setColors(viewHolder, convertView, info, res, -1, R.color.white,
					R.color.white, R.color.gray);
			break;
		default:
			break;
		}

		if (selectedPosition == position) {
			setColors(viewHolder, convertView, info, res, -1, R.color.white,
					R.color.white, R.color.red);
		}
		viewHolder.date.setText(info.getDate() + "");
		if (null != info.getRemark()) {
			viewHolder.remark.setText(info.getRemark());
		} else {
			viewHolder.remark.setText("");
		}

		int width = DataUtils.getScreenWidth(activity);
		// int height = DataUtils.getScreenHeight(activity);
		viewHolder.date.setTextSize(TypedValue.COMPLEX_UNIT_PX, width / 20);
		viewHolder.remark.setTextSize(TypedValue.COMPLEX_UNIT_PX, width / 32);

		convertView.setLayoutParams(new GridView.LayoutParams(
				LayoutParams.MATCH_PARENT,
				DataUtils.getScreenWidth(activity) / 7));
		return convertView;
	}

	private void setColors(ViewHolder viewHolder, View convertView,
			DateInfo info, Resources res, int remarkStr, int dateColor,
			int remarkColor, int detailsColor) {
		if (remarkStr != -1) {
			info.setRemark(res.getString(remarkStr));
		}
		viewHolder.date.setTextColor(res.getColor(dateColor));
		viewHolder.remark.setTextColor(res.getColor(remarkColor));
		convertView.setBackgroundColor(res.getColor(detailsColor));
	}

	private class ViewHolder {
		TextView date;
		TextView remark;
	}

}
